<template>
  <div>
    <el-form
      ref="form"
      label-position="top"
      :model="form"
      :disabled="isReadOnly"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            :label="$t('business.workload.restart_strategy')"
            prop="restartPolicy"
          >
            <ko-data-item
              v-if="isJobs()"
              v-model="form.restartPolicy"
              itemType="radio"
              :radios="restart_policy_list_job"
            />
            <ko-data-item
              v-else
              v-model="form.restartPolicy"
              itemType="radio"
              :radios="restart_policy_list_common"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('business.workload.termination_grace_period')"
            prop="terminationGracePeriodSeconds"
          >
            <ko-data-item
              v-model.number="form.terminationGracePeriodSeconds"
              :dividerName="$t('business.workload.seconds')"
              itemType="number"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            :label="$t('business.workload.pull_secrets')"
            prop="imagePullSecrets"
          >
            <ko-data-item
              v-model="form.imagePullSecrets"
              item-ype="flatSelect"
              multiple
              :selections="secret_list"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('business.workload.service_account')"
            prop="serviceAccountName"
          >
            <ko-data-item
              v-model="form.serviceAccountName"
              item-ype="flatSelect"
              :selections="service_list"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import KoDataItem from "@/views/domain/kubeHud/views/components/ko-data-item/index.vue";

export default {
  name: "KoSpecBase",
  components: { KoDataItem },
  props: {
    specBaseParentObj: Object,
    serviceList: Array,
    secretList: Array,
    repoList: Array,
    resourceType: String,
    isReadOnly: Boolean
  },
  data() {
    return {
      form: {
        restartPolicy: "",
        imagePullSecrets: [],
        serviceAccountName: "",
        terminationGracePeriodSeconds: ""
      },
      restart_policy_list_common: [
        { label: "Always", value: "Always" },
        { label: "OnFailure", value: "OnFailure" },
        { label: "Never", value: "Never" }
      ],
      restart_policy_list_job: [
        { label: "OnFailure", value: "OnFailure" },
        { label: "Never", value: "Never" }
      ],
      service_list: [],
      secret_list: []
    };
  },
  watch: {
    serviceList: {
      handler(newName) {
        this.service_list = [];
        if (newName) {
          this.service_list = newName;
        }
      },
      immediate: true
    },
    secretList: {
      handler(newName) {
        this.secret_list = [];
        if (newName) {
          for (const s of newName) {
            this.secret_list.push(s.metadata.name);
          }
        }
      },
      immediate: true
    },
    specBaseParentObj: {
      handler(newObj) {
        this.form.imagePullSecrets = [];
        if (newObj.imagePullSecrets) {
          for (const sec of newObj.imagePullSecrets) {
            this.form.imagePullSecrets.push(sec.name);
          }
        }
        if (this.specBaseParentObj.restartPolicy) {
          this.form.restartPolicy = this.specBaseParentObj.restartPolicy;
        } else {
          this.form.restartPolicy = this.isJobs() ? "OnFailure" : "Always";
        }
        if (this.specBaseParentObj.serviceAccountName) {
          this.form.serviceAccountName =
            this.specBaseParentObj.serviceAccountName;
        }
        if (this.specBaseParentObj.terminationGracePeriodSeconds) {
          this.form.terminationGracePeriodSeconds =
            this.specBaseParentObj.terminationGracePeriodSeconds;
        }
      },
      immediate: true
    }
  },
  methods: {
    isJobs() {
      return this.resourceType === "jobs" || this.resourceType === "cronjobs";
    },
    transformation(parentFrom, metadata) {
      parentFrom.restartPolicy = this.form.restartPolicy || undefined;
      parentFrom.serviceAccountName = this.form.serviceAccountName || undefined;
      parentFrom.terminationGracePeriodSeconds =
        this.form.terminationGracePeriodSeconds || undefined;

      let imagePullSecrets = [];
      for (const item of this.form.imagePullSecrets) {
        imagePullSecrets.push({ name: item });
      }
      if (!metadata.labels) {
        parentFrom.imagePullSecrets =
          imagePullSecrets.length !== 0 ? imagePullSecrets : undefined;
        return;
      }
      for (const key in metadata.labels) {
        if (key.indexOf("kubepi-repo-") !== -1) {
          let secretName = "kubepi-" + metadata.labels[key] + "-secret";
          if (!this.existSecret(secretName, imagePullSecrets)) {
            imagePullSecrets.push({ name: secretName });
          }
        }
      }
      parentFrom.imagePullSecrets =
        imagePullSecrets.length !== 0 ? imagePullSecrets : undefined;
      return;
    },
    existSecret(secretName, imagePullSecrets) {
      if (imagePullSecrets.length === 0) {
        return false;
      }
      for (const sec of imagePullSecrets) {
        if (sec.name === secretName) {
          return true;
        }
      }
      return false;
    }
  }
};
</script>
